
Public Class BackUpToDevice

    Private MainDir As String
    Private DbDir As String
    Private PrjDir As String

    Private myDriveLetter As String

    Private myDatabases() As sSQLServer
    Private myProjects() As Project

    Public Property DriveLetter() As String
        Get
            Return Me.myDriveLetter
        End Get
        Set(ByVal value As String)
            Me.myDriveLetter = value
        End Set
    End Property
    Public Property Projects() As Project()
        Get
            Return Me.myProjects
        End Get
        Set(ByVal value As Project())
            Me.myProjects = value
        End Set
    End Property
    Public Property Databases() As sSQLServer()
        Get
            Return Me.myDatabases
        End Get
        Set(ByVal value As sSQLServer())
            Me.myDatabases = value
        End Set
    End Property

    Public Sub New()

    End Sub

    Public Sub Start()

        If Me.ValidProperties = False Then Throw New Exception(Messages.InvalidDeviceProperties)

        Me.CreateDirectoryStructure()
        Me.BackupDataBases()
        Me.BackupProjects()

        Console.WriteLine("")
        Console.WriteLine("Finished backing up project(s)")

    End Sub

    Private Function ValidProperties() As Boolean
        'TODO: Validate removable device properties
        Return True
    End Function

    Private Sub CreateDirectoryStructure()

        MainDir = myDriveLetter & "Sync"
        DbDir = MainDir & "\db"
        PrjDir = MainDir & "\prj"

        Try
            IO.Directory.Delete(MainDir, True)
        Catch ex As Exception

        End Try

        Try

            IO.Directory.CreateDirectory(MainDir)

        Catch ex As Exception
            Throw New Exception(Messages.ErrorCreatingDirectoryStructure, ex)
        End Try

    End Sub

    Private Sub BackupDataBases()

        Dim sSQL As sSQLServer
        Dim i As Integer = 0

        For Each sSQL In Me.myDatabases

            Dim dr As String = DbDir & i

            Console.WriteLine("Backing up: " & sSQL.Database)
            IO.Directory.CreateDirectory(dr)
            Dim bck As New SQL(sSQL.ServerName, sSQL.User, sSQL.Password, sSQL.Database, dr)

            i = i + 1
        Next

    End Sub

    Private Sub BackupProjects()

        Dim pj As Project
        Dim i As Integer = 0

        For Each pj In Me.myProjects

            Dim dr As String = PrjDir & i

            Console.WriteLine("Backing up: " & pj.ProjectDirectory)
            IO.Directory.CreateDirectory(dr)
            DirCopy.RecursiveCopyFiles(pj.ProjectDirectory, dr, True, True)

            i = i + 1
        Next

    End Sub

End Class
